<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Silence
 * Date: 3/8/12
 * Time: 3:50 AM
 * To change this template use File | Settings | File Templates.
 */

require_once('constants.php');
require_once('api.php');

$con = db_prepare();

$sql = "DROP TABLE IF EXISTS `folder`";
db_die(mysql_query($sql));

$sql = "DROP TABLE IF EXISTS `event`";
db_die(mysql_query($sql));

$sql = "DROP TABLE IF EXISTS `event_event`";
db_die(mysql_query($sql));

$sql = "CREATE TABLE IF NOT EXISTS `folder` (
  `fid` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'folder id',
  `pid` bigint(20) UNSIGNED NOT NULL COMMENT 'parent folder id, 0 for root',
  `name` varchar(255) NOT NULL COMMENT 'folder name',
  `status` int(11) UNSIGNED NOT NULL DEFAULT '1' COMMENT '1 for normal, 0 for deleted.',
  PRIMARY KEY (`fid`)
)";
db_die(mysql_query($sql));


$sql = "CREATE TABLE IF NOT EXISTS `event` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'event id',
  `name` varchar(255) NOT NULL COMMENT 'event name',
  `start_date` varchar(255) NOT NULL COMMENT 'format: yyyy-mm-dd',
  `end_date` varchar(255) NOT NULL COMMENT 'format: yyyy-mm-dd',
  `status` int(11) UNSIGNED NOT NULL DEFAULT '1' COMMENT '1 for normal, 0 for deleted.',
  PRIMARY KEY (`id`)
)";
db_die(mysql_query($sql));


$sql = "CREATE TABLE IF NOT EXISTS `event_event` (
    `pid` bigint(20) UNSIGNED NOT NULL COMMENT 'parent event id',
    `cid` bigint(20) UNSIGNED NOT NULL COMMENT 'child event id',
    PRIMARY KEY (`pid`, `cid`)
)";
db_die(mysql_query($sql));


$data = array();
$data[] = array(1, 0, 'CS2103T');
$data[] = array(2, 1, 'Planning');
$data[] = array(3, 1, 'Implementation Phase 1');
$data[] = array(4, 1, 'Implementation Phase 2');
$data[] = array(5, 1, 'Production Phase 1');
$data[] = array(6, 3, 'Build prototype');


foreach ($data as $val) {
    $fid = $val[0];
    $pid = $val[1];
    $name = "'" . $val[2] . "'";
    $sql = "INSERT INTO `folder` (`fid`, `pid`, `name`) VALUES ($fid, $pid, $name)";
//    print $sql;
    db_die(mysql_query($sql));
}

$data = json_decode('[
    {
        "EndDate":"2010-02-02",
        "Name":"Planning",
        "StartDate":"2010-01-18",
        "expanded":false,
        "Id":3,
        "children":[
            {
                "EndDate":"2010-01-28",
                "Id":11,
                "leaf":true,
                "Name":"Investigate",
                "Responsible":"",
                "StartDate":"2010-01-18"
            },
            {
                "EndDate":"2010-02-02",
                "Id":12,
                "leaf":true,
                "Name":"Assign resources",
                "StartDate":"2010-01-28"
            },
            {
                "EndDate":"2010-02-02",
                "Id":13,
                "leaf":true,
                "Name":"Gather documents",
                "StartDate":"2010-01-25"
            },
            {
                "EndDate":"2010-02-02",
                "Id":17,
                "leaf":true,
                "Name":"Report to management",
                "StartDate":"2010-02-02"
            }
        ]
    },
    {
        "EndDate":"2010-03-20",
        "Id":4,
        "Name":"Implementation Phase 1",
        "StartDate":"2010-01-25",
        "expanded":true,
        "children":[
            {
                "EndDate":"2010-01-30",
                "Id":34,
                "leaf":true,
                "Name":"Preparation work",
                "StartDate":"2010-01-25"
            },
            {
                "EndDate":"2010-03-07",
                "ba":"2010-03-07",
                "Id":14,
                "leaf":true,
                "Name":"Evaluate chipsets",
                "StartDate":"2010-02-25"
            },
            {
                "EndDate":"2010-03-20",
                "Id":16,
                "leaf":true,
                "Name":"Choose technology suite",
                "StartDate":"2010-03-10"
            },
            {
                "EndDate":"2010-03-08",
                "Id":15,
                "Name":"Build prototype",
                "StartDate":"2010-01-30",
                "expanded":true,
                "children":[
                    {
                        "EndDate":"2010-02-09",
                        "Id":20,
                        "leaf":true,
                        "Name":"Step 1",
                        "StartDate":"2010-01-30"
                    },
                    {
                        "EndDate":"2010-02-22",
                        "Id":19,
                        "leaf":true,
                        "Name":"Step 2",
                        "StartDate":"2010-02-17"
                    },
                    {
                        "EndDate":"2010-03-07",
                        "Id":18,
                        "leaf":true,
                        "Name":"Step 3",
                        "StartDate":"2010-02-25"
                    },
                    {
                        "EndDate":"2010-03-08",
                        "Id":21,
                        "leaf":true,
                        "Name":"Follow up with customer",
                        "StartDate":"2010-03-04"
                    }
                ]
            }
        ]
    },
    {
        "EndDate":"2010-03-08",
        "Id":5,
        "leaf":true,
        "Name":"Customer approval",
        "StartDate":"2010-03-08"
    },
    {
        "EndDate":"2010-03-18",
        "Id":6,
        "Name":"Implementation Phase 2",
        "StartDate":"2010-03-08",
        "expanded":true,
        "children":[
            {
                "EndDate":"2010-03-18",
                "Id":25,
                "leaf":true,
                "Name":"Task 3",
                "StartDate":"2010-03-08"
            },
            {
                "EndDate":"2010-03-18",
                "Id":26,
                "leaf":true,
                "Name":"Task 2",
                "StartDate":"2010-03-08"
            },
            {
                "EndDate":"2010-03-18",
                "Id":27,
                "leaf":true,
                "Name":"Task 1",
                "StartDate":"2010-03-08"
            }
        ]
    },
    {
        "EndDate":"2010-03-17",
        "Id":10,
        "leaf":true,
        "Name":"Customer approval 2",
        "StartDate":"2010-03-17"
    },
    {
        "EndDate":"2010-05-08",
        "Id":8,
        "Name":"Production phase 1",
        "StartDate":"2010-03-22",
        "expanded":true,
        "children":[
            {
                "EndDate":"2010-04-07",
                "Id":22,
                "leaf":true,
                "Name":"Assemble",
                "StartDate":"2010-03-22"
            },
            {
                "EndDate":"2010-04-21",
                "Id":23,
                "leaf":true,
                "Name":"Load SW",
                "StartDate":"2010-04-06"
            },
            {
                "EndDate":"2010-05-08",
                "Id":24,
                "leaf":true,
                "Name":"Basic testing (inc some test)",
                "StartDate":"2010-04-22"
            }
        ]
    },
    {
        "EndDate":"2010-05-15",
        "Id":9,
        "leaf":true,
        "Name":"Final testing",
        "StartDate":"2010-05-07"
    },
    {
        "EndDate":"2010-05-15",
        "Id":7,
        "leaf":true,
        "Name":"Delivery",
        "StartDate":"2010-05-15"
    }
]');

print_r($data);

insert_recur($data, MYFOLDER_FID);


function insert_recur($data, $pid) {
    foreach($data as $row) {
        $sql = "INSERT INTO `event` (`id`, `start_date`, `end_date`, `name`) VALUES
        ($row->Id, '$row->StartDate', '$row->EndDate', '$row->Name')";
        db_die(mysql_query($sql));

        $sql = "INSERT INTO `event_event` (`pid`, `cid`) VALUES
        ($pid, $row->Id)";
        db_die(mysql_query($sql));

        if (isset($row->children)) {
            insert_recur($row->children, $row->Id);
        }
    }
}


mysql_close($con);